// import parseTime, formatTime and set to filter
import platform from '@/config/house/platform';
import format from 'date-fns/format';
export { parseTime, formatTime } from '@/utils';

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label;
  }
  return time + label + 's';
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time);
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute');
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour');
  } else {
    return pluralize(~~(between / 86400), ' day');
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1e18, symbol: 'E' },
    { value: 1e15, symbol: 'P' },
    { value: 1e12, symbol: 'T' },
    { value: 1e9, symbol: 'G' },
    { value: 1e6, symbol: 'M' },
    { value: 1e3, symbol: 'k' }
  ];
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol;
    }
  }
  return num.toString();
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

export function getActiveStr(str = '', total = 8) {
  let realLength = 0;
  str = String(str);
  const len = str.length;
  let result = '';
  if (len === 0) {
    return '';
  }
  for (let i = 0; i < len; i++) {
    if (str.charCodeAt(i) > 128) {
      realLength += 2;
    } else {
      realLength += 1;
    }
    if (realLength > total) {
      return result + '...';
    }
    result = result + str.charAt(i);
  }
  return result;
}

function add0(m) {
  return m < 10 ? '0' + m : m;
}

export function parseDate(timestamp, hideDetail = true) {
  if (!timestamp) return '';
  var time = new Date(timestamp);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  if (!hideDetail) {
    return y + '-' + add0(m) + '-' + add0(d);
  }
  var h = time.getHours();
  var mm = time.getMinutes();
  var s = time.getSeconds();
  return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

export function empty(str) {
  return str || '--';
}

export function w(n) {
  return n / 10000;
}

export function dateFormat(value, formatStr = 'yyyy-MM-dd HH:mm:ss') {
  if(!value) return ''
  return format(value, formatStr);
}

export function sizeFormat(value) {
  return value ? '面积：' + value + '平米' : '面积未锁定';
}

export function getPortTypeName(value) {
  return platform[value];
}
